package guestbook;

import javax.servlet.http.*;
import com.google.appengine.api.datastore.*;

public class Context
{
	public final HttpServletRequest request;
	public final HttpServletResponse response;
	
	public final PutHelper putHelper;
	public final Tally tally;

	public Context(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		this.request = request;
		this.response = response;

		if (Tally.ON)
			this.tally = new Tally(this);

		putHelper = new PutHelper(this);
	}

	public String getCurrentPage()
	{
		// Should return the most info about the current request, getServletPath
		// will not work well for AJAX. Used when priting our tallied results.
		return request.getServletPath();
	}

	public LDatastoreService getDatastoreService()
	{
		return new LDatastoreService(DatastoreServiceFactory.getDatastoreService(DatastoreServiceConfig.Builder.withReadPolicy(new ReadPolicy(
				ReadPolicy.Consistency.EVENTUAL))), this);
	}

	public LAsyncDatastoreService getAsyncDatastoreService()
	{
		return new LAsyncDatastoreService(DatastoreServiceFactory.getAsyncDatastoreService(DatastoreServiceConfig.Builder.withReadPolicy(new ReadPolicy(
				ReadPolicy.Consistency.EVENTUAL))), this);
	}
}